# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=godot
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=3.3.4
pkgrel=1
pkgdesc='An advanced, feature packed, multi-platform 2D and 3D game engine (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.godotengine.org/"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-embree"
        #  "${MINGW_PACKAGE_PREFIX}-bullet"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libvpx"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-mbedtls"
         "${MINGW_PACKAGE_PREFIX}-miniupnpc"
         "${MINGW_PACKAGE_PREFIX}-opusfile"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-yasm"
             "${MINGW_PACKAGE_PREFIX}-scons")
options=('!emptydirs' 'staticlibs' 'strip')
source=("https://github.com/godotengine/godot/releases/download/${pkgver}-stable/${_realname}-${pkgver}-stable.tar.xz"
        "001-force-using-pkgconfig.patch")
sha256sums=('9f53a7d6d63f4e9d793c41ad1bef89518619f3e1f0ddd089a6521f2d5edc5859'
            '771fe57921f0e5cfd0577eb13a23014bf1a04f2ed579594aef14530ac6aeb344')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}-stable"

  # Copied from platform/x11/detect.py
  patch -p1 -i "${srcdir}/001-force-using-pkgconfig.patch"
}

build() {
  msg "Build for ${MSYSTEM}"
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  cp -r "${_realname}-${pkgver}-stable" "build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  # Copied from Arch
  # bullet: outdated <2.89
  local to_unbundle="embree freetype libogg libpng libtheora libvorbis libvpx libwebp mbedtls miniupnpc opus pcre2 zlib zstd"
  local system_libs=""
  for _lib in $to_unbundle; do
    system_libs+="builtin_"$_lib"=no "
    rm -rf thirdparty/$_lib
  done

  declare -a extra_config
  if [ "$CARCH" = "x86_64" ]; then
    extra_config+=("use_lto=yes")
    extra_config+=("bits=64")
    extra_config+=("mingw_prefix_64=${MINGW_PREFIX}/bin/")
  else
    # lto is broken on 32bit
    extra_config+=("use_lto=no")
    extra_config+=("bits=32")
    extra_config+=("mingw_prefix_32=${MINGW_PREFIX}/bin/")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    extra_config+=("use_llvm=yes")
    extra_config+=("use_thinlto=yes")
  else
    extra_config+=("use_llvm=no")
  fi

  ${MINGW_PREFIX}/bin/scons ${MAKEFLAGS} \
    platform=windows \
    target=release_debug \
    optimize=speed \
    use_mingw=yes \
    use_static_cpp=no \
    "${extra_config[@]}" \
    tools=yes \
    ${system_libs} \
    verbose=yes
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  install -Dm755 bin/godot.windows.opt.tools.*.exe "${pkgdir}${MINGW_PREFIX}/bin/godot.exe"
  install -Dm644 icon.svg "${pkgdir}${MINGW_PREFIX}/share/pixmaps/godot.svg"
  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/godot/LICENSE.txt"
  install -D -m644 "misc/dist/linux/godot.6" "${pkgdir}${MINGW_PREFIX}/share/man/man6/godot.6"
}
